import EnterpriseAdminExtensionAbility from '@ohos.enterprise.EnterpriseAdminExtensionAbility';
import Logger from '../common/logger'
import Notification from '@ohos.notificationManager';
import notification from '@ohos.notification';

export default class EnterpriseAdminAbility extends EnterpriseAdminExtensionAbility {
    static countEnable: number = 0;
    static countDisable: number = 0;
    static countBundleAdd: number = 0;
    static countBundleRemove: number = 0;

    onAdminEnabled() {
        // Ability has back to background
        EnterpriseAdminAbility.countEnable++;
        Logger.info("EnterpriseAdminAbility", "onAdminEnabled countEnable=" + EnterpriseAdminAbility.countEnable +
        ' | countDisable=' + EnterpriseAdminAbility.countDisable)
        this.pushNotification("设备管理器已激活")
    }

    onAdminDisabled() {
        // Ability has back to background
        EnterpriseAdminAbility.countDisable++;
        Logger.info("EnterpriseAdminAbility", "onAdminDisabled countEnable=" + EnterpriseAdminAbility.countEnable +
        ' | countDisable=' + EnterpriseAdminAbility.countDisable)
        this.pushNotification("设备管理器去激活")
    }

    onBundleAdded(bundleName: string) {
        EnterpriseAdminAbility.countBundleAdd++;
        Logger.info("EnterpriseAdminAbility", "onBundleAdded bundleName:" + bundleName + " | countBundleAdded:" + EnterpriseAdminAbility.countBundleAdd + "| countBundleRemove:" + EnterpriseAdminAbility.countBundleRemove)
        this.pushNotification(bundleName + "安装")
    }

    onBundleRemoved(bundleName: string) {
        EnterpriseAdminAbility.countBundleRemove++;
        Logger.info("EnterpriseAdminAbility", "onBundleRemoved bundleName" + bundleName + " | countBundleAdded:" + EnterpriseAdminAbility.countBundleAdd + " | countBundleRemove:" + EnterpriseAdminAbility.countBundleRemove)
        this.pushNotification(bundleName + "卸载")
    }

    onAppStart(bundleName: string) {
        EnterpriseAdminAbility.countBundleRemove++;
        Logger.info("EnterpriseAdminAbility", "onBundleRemoved bundleName" + bundleName + " | countBundleAdded:" + EnterpriseAdminAbility.countBundleAdd + " | countBundleRemove:" + EnterpriseAdminAbility.countBundleRemove)
        this.pushNotification(bundleName + "启动")
    }

    onAppStop(bundleName: string) {
        EnterpriseAdminAbility.countBundleRemove++;
        Logger.info("EnterpriseAdminAbility", "onBundleRemoved bundleName" + bundleName + " | countBundleAdded:" + EnterpriseAdminAbility.countBundleAdd + " | countBundleRemove:" + EnterpriseAdminAbility.countBundleRemove)
        this.pushNotification(bundleName + "停止")
    }

    onStart() {
        Logger.info("EnterpriseAdminAbility onStart")
        this.pushNotification("EnterpriseAdminAbility onStart")
    }

    pushNotification(message:string) {
        let notificationRequest: Notification.NotificationRequest = {
            id: 1,
            content: {
                contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
                normal: {
                    title: "EDM通知",
                    text: message,
                }
            }
        };
        Notification.publish(notificationRequest);
    }
};
